package fancy.calendar.dataaccess;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import fancy.calendar.database.DBConnection;
import fancy.calendar.dataobject.FancyCalendarItem;
import fancy.calendar.tools.Tools;

/**
 * Data base access object
 * 
 * @author wannadream
 * 
 */
public class CalendarItemDataAccess {

	private CalendarItemDataAccess() {

	}

	private static void sqlPrint(PreparedStatement stat) {
		System.out.println(stat.toString());
	}

	public static ArrayList<FancyCalendarItem> getCalendarItemList(Date today)
			throws SQLException, IOException, ClassNotFoundException {
		String sql_select = "select * from t_calendar_item where date = ? order by date asc, time asc";
		ArrayList<FancyCalendarItem> list = new ArrayList<FancyCalendarItem>();
		DBConnection dbconn = new DBConnection();
		Connection conn = dbconn.getConnection();
		PreparedStatement stat = conn.prepareStatement(sql_select);
		stat.setDate(1, new java.sql.Date(today.getTime()));
		sqlPrint(stat);
		ResultSet rs = stat.executeQuery();
		while (rs.next()) {
			FancyCalendarItem item = new FancyCalendarItem();
			item.setId(rs.getInt(1));
			item.setDate(Tools.transferSqlDate(rs.getDate(2)));
			item.setTime(rs.getString(3));
			item.setContent(rs.getString(4));
			list.add(item);
		}

		rs.close();
		stat.close();
		conn.close();
		return list;

	}

}
